package com.example.controller;

import com.example.pojo.Users;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@Controller
public class UsersController {

    private static final Logger logger = LoggerFactory.getLogger(UsersController.class);

    @RequestMapping("addUser")
    public String showPage(@ModelAttribute("users") Users users) {
        return "add";
    }

    @RequestMapping("save")
    public String saveUser(@ModelAttribute("users") @Valid Users users, BindingResult result) {
        if (result.hasErrors()) {
            return "add";
        }

        logger.info("add user:{}", users);
        return "ok";
    }

}
